﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;

namespace TopNewLib.TopNews
{
    public class BaiduTopClass : BaseTop
    {
        public BaiduTopClass()
        {
            LinkURL = "http://top.baidu.com/buzz?b=1&fr=topindex";
        }

        public BaiduTopClass(string _linkUrl)
        {
            LinkURL = _linkUrl;
        }

        public override List<TopNewItem> GetCurTopNews()
        {
            List<TopNewItem> rs = new List<TopNewItem>();

            string htmlstr = GetHtmlInfo();

            var doc = new HtmlDocument();
            doc.LoadHtml(htmlstr);

            var res = doc.DocumentNode.SelectSingleNode("//*[@id=\"main\"]/div[2]/div/table");
            if (res != null)
            {
                var list1 = res.SelectNodes("//td[@class='first']");//选择标签数组
                var list2 = res.SelectNodes("//td[@class='keyword']/a[1]");//选择标签数组
                var list3 = res.SelectNodes("//td[@class='last']/span");//选择标签数组
                if (list1.Count > 0 && list1.Count == list2.Count && list2.Count == list3.Count)
                {
                    for (int i = 0; i < list1.Count;i++ )
                    {
                        TopNewItem item = new TopNewItem();

                        item.Title = list2[i].InnerText;
                        item.Url = list2[i].Attributes["href"].Value;
                        item.Describe = list3[i].InnerText;

                        rs.Add(item);
                    }
                }
            }

            return rs;
        }


    }
}
